package cc.mrbird.common.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan("cc.mrbird.system.*.dao")
public class MybatisPlusConfig {

    /**
     * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
     * 乐观锁（仅支持 updateById(id) 与 update(entity, wrapper) 方法）
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //向Mybatis过滤器链中添加分页拦截器
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        //向Mybatis过滤器链中添加乐观锁拦截器
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        //还可以添加i他的拦截器
        return interceptor;
    }

/*    *//**
     * 添加pagehelper分页
     **//*
    @Bean
    public ConfigurationCustomizer mybatisConfigurationCustomizer() {
        return new ConfigurationCustomizer() {
            @Override
            public void customize(MybatisConfiguration configuration) {
                PageInterceptor pageInterceptor=new PageInterceptor();
                Properties properties = new Properties();
                //数据库
                properties.setProperty("autoRuntimeDialect", "true");
                //是否将参数offset作为PageNum使用
                properties.setProperty("offsetAsPageNum", "true");
                //是否进行count查询
                properties.setProperty("rowBoundsWithCount", "true");
                //是否分页合理化
                properties.setProperty("reasonable", "true");
                //如果 pageSize=0 或者 RowBounds.limit = 0 就会查询出全部的结果
                properties.setProperty("pageSizeZero", "true");
                pageInterceptor.setProperties(properties);
                configuration.addInterceptor(pageInterceptor);
            }
        };
    }*/

}